class Admin::ServiceItemsController < AdminController
  def index
    @service_items = ServiceItem.page(params[:page]).per(params[:per])
  end

  def new
    @service_item = ServiceItem.new
  end

  def create
    @service_item = ServiceItem.new service_item_params
    if @service_item.save
      redirect_to admin_service_items_path, notice: "保存成功！"
    else
      redirect_to admin_service_items_path, notice: "保存失败！"
    end
  end

  def edit
    @service_item = ServiceItem.find params[:id]
  end

  def update
    @service_item = ServiceItem.find params[:id]
    if @service_item.update service_item_params
      redirect_to admin_service_items_path, notice: "更新成功！"
    else
      redirect_to admin_service_items_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @service_item = ServiceItem.find params[:id]
    if @service_item.destroy
      redirect_to admin_service_items_path, notice: "删除成功."
    else
      redirect_to admin_service_items_path, notice: "删除失败."
    end
  end

  private

  def service_item_params
    params.require(:service_item).permit(:avatar, :name, :description)
  end
end
